#include "canreadthread.h"
#include "QDebug"
#include <QTime>
#include "canset.h"
#include "CANCard/GC/CANPar.h"
#include "FCC300_OperFun.h"
CanReadThread::CanReadThread(QObject *Parent):
    QThread(Parent)
{
    number=0;
}
void CanReadThread::run()
{
    QByteArray ba;
    ba.resize(8);
    QByteArray CAN2_ba;
    CAN2_ba.resize(8);
    while(1)
    {
        usleep(20);
        QDateTime  s_t=QDateTime::currentDateTime();
        QString systime =s_t.toString("dd-MM-yyyy HH:mm:ss");
        dwRel_read= Receive(nDeviceType, nDeviceInd, 0, vco, 100, 10);
        can2_dwRel_read= Receive(nDeviceType, nDeviceInd, 1, CAN2_vco, 100, 10);
        if(dwRel_read==0xFFFFFFFF)
        {
            qDebug()<<"CAN0读取数据错误";
            //ReadErrInfo()
        }
        if(can2_dwRel_read==0xFFFFFFFF)
        {
            qDebug()<<"CAN1读取数据错误";
            //ReadErrInfo()
        }
        if(dwRel_read !=0 && dwRel_read != 0xFFFFFFFF)
        {

            for (uint i = 0; i < dwRel_read; ++i)             ///对每一个接收到的数据进行处理
            {
                RE_CAN1_ID=vco[i].ID;// & 0x1fffffff; //前三位是标志位所以需要去掉
                for(int a=0;a<8;++a)
                {
                    RE_CAN1_Data[a]=vco[i].Data[a];
                    ba[a]=RE_CAN1_Data[a];
                }
                RE_CAN1_Time=vco[i].TimeStamp;


                QString can_timer=QTime(0, 0, 0).addSecs(int(RE_CAN1_Time/100000)).toString(QString::fromLatin1("HH:mm:ss"));
                QString str_can1id = QString::number(RE_CAN1_ID, 16);  //将接收到的CANID转16进制
                QString data_can1= ba.toHex();
                column_can=list_CANID.indexOf(str_can1id);
                if (column_can == -1)  //column_can = -1代表索引不到表中没有
                {
                    list_CANID.append(str_can1id);
                    column_can=list_CANID.indexOf(str_can1id);       //获取新加ID的行号
                    CAN_RT[column_can]=RE_CAN1_Time;           //对设备时间的数组对应的数据进行赋值
                    CAN_count[column_can]=1;
                }
                else
                {
                    //column_can=list_CANID.indexOf(str_can1id);
                    canDxtime=RE_CAN1_Time-CAN_RT[column_can];
                    CAN_RT[column_can]=RE_CAN1_Time;
                    CAN_RTdex[column_can]=canDxtime/1000;
                    CAN_count[column_can]=CAN_count[column_can]+1;

                }

                // 经过if判断语句 对list_CANID,CAN_RT,CAN_TRTDX,list_CANdata进行了更新。
                QString  addcan_dextime= QString::number(CAN_RTdex[column_can], 10);
                QString  addcan_redtime= QString::number(CAN_RT[column_can], 10);
                QString  addcan_count= QString::number(CAN_count[column_can], 10);
                //第row_k行2+1列添加canid
                CANID_Item =new QStandardItem;
                CANdata_Item =new QStandardItem;
                CANEQtime_Item =new QStandardItem;
                CANEQtimedex_Item =new QStandardItem;
                CANSYStime_Item =new QStandardItem;
                CANcount_Item =new QStandardItem;
                CANRWbit_Item =new QStandardItem;
                CANID_Item->setData(RE_CAN1_ID,Qt::UserRole+1);
                CANID_Item->setData(QString::number(RE_CAN1_ID, 16).toUpper(),Qt::DisplayRole);
                CANdata_Item->setData(ba,Qt::UserRole+1);
                CANdata_Item->setData(ba.toHex().toUpper(),Qt::DisplayRole);
                CANEQtime_Item->setData(CAN_RT[column_can],Qt::UserRole+1);
                CANEQtime_Item->setData(CAN_RT[column_can],Qt::DisplayRole);
                CANSYStime_Item->setData(systime,Qt::UserRole+1);
                CANSYStime_Item->setData(systime,Qt::DisplayRole);
                CANcount_Item->setData(CAN_count[column_can],Qt::UserRole+1);
                CANcount_Item->setData(CAN_count[column_can],Qt::DisplayRole);
                CANRWbit_Item->setData(1,Qt::UserRole);
                CANRWbit_Item->setData(1,Qt::DisplayRole);
                CANEQtimedex_Item->setData(CAN_RTdex[column_can],Qt::UserRole+1);
                CANEQtimedex_Item->setData(CAN_RTdex[column_can],Qt::DisplayRole);
                modelcan.setItem( column_can ,0,CANSYStime_Item);          //0列系统时间
                modelcan.setItem( column_can ,1,CANEQtime_Item);   //第row_k行2+1列添加
                modelcan.setItem( column_can ,2,CANID_Item);       //CANID
                modelcan.setItem( column_can ,3,CANEQtimedex_Item);     //时间间隔
                modelcan.setItem( column_can ,4,CANdata_Item);     //数据
                modelcan.setItem( column_can ,5,CANcount_Item);     //CAN数据计数
                modelcan.setItem( column_can ,6,CANRWbit_Item);     //CAN数据计数

                if(can_save==1)
                {
                    QFile can_file(canfileName);
                    if(!can_file.open(QIODevice::WriteOnly | QIODevice::Append))
                    {
                        qDebug() << "0000000000000000000000";
                    }
                    else
                    {
                        //QString save_data= ba.toHex().toUpper();

                        float  save_time=canDxtime/1000;
                        QTextStream csvOutPut(&can_file);
                        QString str_data;
                        QString str[8];
                        for (int var = 0; var < 8; ++var) {
                            str[var]=QString("%1").arg(RE_CAN1_Data[var],2,16,QLatin1Char('0')).toUpper()+",";
                            str_data=str_data+str[var];

                        }

                        QString str_canid=QString("%1").arg(RE_CAN1_ID,8,16,QLatin1Char('0')).toUpper()+",";

                        csvOutPut <<can_timer<<","<<str_canid<<save_time<<","<<str_data<<"CAN0"<<"\n";
                        can_file.flush();
                        can_file.close();
                        qDebug() << "str[var]"<<str_data;
                    }
                }
                FCC300_OperFun::Can0Data_pro(RE_CAN1_ID,RE_CAN1_Data);
              //  UAV_OperFun::Can0Data_pro(RE_CAN1_ID,RE_CAN1_Data);//加上解析
            }
        }
        if(can2_dwRel_read !=0 && can2_dwRel_read != 0xFFFFFFFF)
        {

            for (uint i = 0; i < can2_dwRel_read; ++i)             ///对每一个接收到的数据进行处理
            {
                RE_CAN2_ID=CAN2_vco[i].ID;// & 0x1fffffff; //前三位是标志位所以需要去掉
                for(int a=0;a<8;++a)
                {
                    RE_CAN2_Data[a]=CAN2_vco[i].Data[a];
                    CAN2_ba[a]=RE_CAN2_Data[a];
                }
                RE_CAN2_Time=CAN2_vco[i].TimeStamp;
                QString can2_timer=QTime(0, 0, 0).addSecs(int(RE_CAN2_Time/100000)).toString(QString::fromLatin1("HH:mm:ss"));
                QString str_can2id = QString::number(RE_CAN2_ID, 16);  //将接收到的CANID转16进制
                QString data_can2= CAN2_ba.toHex();
                column_can2=list_CAN2ID.indexOf(str_can2id);
                if (column_can2 == -1)  //column_can = -1代表索引不到表中没有
                {
                    list_CAN2ID.append(str_can2id);
                    column_can2=list_CAN2ID.indexOf(str_can2id);       //获取新加ID的行号
                    CAN2_RT[column_can2]=RE_CAN2_Time;           //对设备时间的数组对应的数据进行赋值
                    CAN2_count[column_can2]=1;
                }
                else
                {
                    //column_can=list_CANID.indexOf(str_can2id);
                    can2Dxtime=RE_CAN2_Time-CAN2_RT[column_can2];
                    CAN2_RT[column_can2]=RE_CAN2_Time;
                    CAN2_RTdex[column_can2]=can2Dxtime/1000;
                    CAN2_count[column_can2]=CAN2_count[column_can2]+1;

                }
                // 经过if判断语句 对list_CANID,CAN_RT,CAN_TRTDX,list_CANdata进行了更新。
                QString  addcan2_dextime= QString::number(CAN2_RTdex[column_can2], 10);
                QString  addcan2_redtime= QString::number(CAN2_RT[column_can2], 10);
                QString  addcan2_count= QString::number(CAN2_count[column_can2], 10);
                //第row_k行2+1列添加canid
                CAN2ID_Item =new QStandardItem;
                CAN2data_Item =new QStandardItem;
                CAN2EQtime_Item =new QStandardItem;
                CAN2EQtimedex_Item =new QStandardItem;
                CAN2SYStime_Item =new QStandardItem;
                CAN2count_Item =new QStandardItem;
                CAN2RWbit_Item =new QStandardItem;
                CAN2ID_Item->setData(RE_CAN2_ID,Qt::UserRole+1);
                CAN2ID_Item->setData(QString::number(RE_CAN2_ID, 16).toUpper(),Qt::DisplayRole);
                CAN2data_Item->setData(CAN2_ba,Qt::UserRole+1);
                CAN2data_Item->setData(CAN2_ba.toHex().toUpper(),Qt::DisplayRole);
                CAN2EQtime_Item->setData(CAN2_RT[column_can2],Qt::UserRole+1);
                CAN2EQtime_Item->setData(CAN2_RT[column_can2],Qt::DisplayRole);
                CAN2SYStime_Item->setData(systime,Qt::UserRole+1);
                CAN2SYStime_Item->setData(systime,Qt::DisplayRole);
                CAN2count_Item->setData(CAN2_count[column_can2],Qt::UserRole+1);
                CAN2count_Item->setData(CAN2_count[column_can2],Qt::DisplayRole);
                CAN2RWbit_Item->setData(1,Qt::UserRole);
                CAN2RWbit_Item->setData(1,Qt::DisplayRole);
                CAN2EQtimedex_Item->setData(CAN2_RTdex[column_can2],Qt::UserRole+1);
                CAN2EQtimedex_Item->setData(CAN2_RTdex[column_can2],Qt::DisplayRole);
                modelcan2.setItem( column_can2 ,0,CAN2SYStime_Item);          //0列系统时间
                modelcan2.setItem( column_can2 ,1,CAN2EQtime_Item);   //第row_k行2+1列添加
                modelcan2.setItem( column_can2 ,2,CAN2ID_Item);       //CANID
                modelcan2.setItem( column_can2 ,3,CAN2EQtimedex_Item);     //时间间隔
                modelcan2.setItem( column_can2 ,4,CAN2data_Item);     //数据
                modelcan2.setItem( column_can2 ,5,CAN2count_Item);     //CAN数据计数
                modelcan2.setItem( column_can2 ,6,CAN2RWbit_Item);     //CAN数据计数


                if(can_save==1)
                {
                    QFile can_file(canfileName);
                    if(!can_file.open(QIODevice::WriteOnly | QIODevice::Append))
                    {
                        qDebug() << "0000000000000000000000";
                    }
                    else
                    {
                        //QString save_data= ba.toHex().toUpper();

                        float  save2_time=can2Dxtime/1000;
                        QTextStream csvOutPut(&can_file);
                        QString str2_data;
                        QString str2[8];
                        for (int var = 0; var < 8; ++var) {
                            str2[var]=QString("%1").arg(RE_CAN2_Data[var],2,16,QLatin1Char('0')).toUpper()+",";
                            str2_data=str2_data+str2[var];

                        }

                        QString str2_canid=QString("%1").arg(RE_CAN2_ID,8,16,QLatin1Char('0')).toUpper()+",";

                        csvOutPut <<can2_timer<<","<<str2_canid<<save2_time<<","<<str2_data<<"CAN1"<<"\n";
                        can_file.flush();
                        can_file.close();
                    }
                }
               // UAV_OperFun::Can1Data_pro(RE_CAN2_ID,RE_CAN2_Data);//加上解析
            }



        }


    }
}


void CanReadThread::CrdtSlot()
{
    number=0;
    emit CrdtSignal(number);
}
